﻿Public Class Veterinaire_CTRL
    Implements IDisposable

    Private _EDM As AniFormEntities
    Private _indexCourant As Integer
    Private _listeVeto As List(Of Veterinaires)

#Region "Cycle de vie du controleur Veterinaire"

    Public Sub New(ByVal edm As AniFormEntities)
        _EDM = edm
        _listeVeto = New List(Of Veterinaires)
        Dim requete As IEnumerable(Of Veterinaires) = (From unVeto In _EDM.Veterinaires _
                                                       Select unVeto _
                                                       Where unVeto.Archive = False)
        _listeVeto.AddRange(requete.ToList)
        _indexCourant = 0
    End Sub

    Private disposedValue As Boolean = False        ' Pour détecter les appels redondants

    ' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
            End If

            '_EDM.Dispose()
            _indexCourant = Nothing
            For Each element In _listeVeto
                element = Nothing
            Next
            _listeVeto.Clear()
            _listeVeto = Nothing
        End If
        Me.disposedValue = True
    End Sub


    Public Sub Dispose() Implements IDisposable.Dispose
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub

#End Region

    Public Sub Enregistrer(ByVal nom As String, ByVal pass As String)
        Dim veto As Veterinaires
        veto = Veterinaires.CreateVeterinaires(Guid.NewGuid, nom, pass, False)
        _EDM.AddToVeterinaires(veto)
        _EDM.SaveChanges(True)
    End Sub

    Public Sub Archiver()
        _listeVeto(_indexCourant).Archiver()
        _listeVeto.RemoveAt(_indexCourant)
        If _listeVeto.Count = _indexCourant Then
            _indexCourant -= 1
        End If
        _EDM.SaveChanges()
    End Sub

    Public Function sidentifier(ByVal nom As String, ByVal password As String) As Boolean
        'Renvoi true si le nom et le prenom sont bien ds la base de données et correspondent
        sidentifier = False

        Dim requete As IEnumerable = (From unveto In _EDM.Veterinaires Select unveto Where unveto.NomVeto = nom And unveto.MotPasse = password).AsEnumerable

        If requete(0) IsNot Nothing Then
            sidentifier = True
            Main_CTRL.getInstance.vetoIdentifie = requete(0)
        End If

    End Function

    Public Sub deconnexion()

        Main_CTRL.getInstance.vetoIdentifie = Nothing

    End Sub

    Public Property indexCourant() As Integer
        Get
            Return _indexCourant
        End Get
        Set(ByVal value As Integer)
            _indexCourant = value
        End Set
    End Property

    Public ReadOnly Property indiceMax() As Integer
        Get
            Return _listeVeto.Count - 1
        End Get
    End Property

    Public ReadOnly Property vetoCourant() As Veterinaires
        Get
            Return _listeVeto(_indexCourant)
        End Get
    End Property

    Public ReadOnly Property isVetoCourantArchivable() As Boolean
        Get
            Dim veto As Veterinaires = _listeVeto(_indexCourant)
            Dim requete As IEnumerable(Of Consultations) = (From uneConsult In _EDM.Consultations _
                                                           Select uneConsult _
                                                           Where uneConsult.Veterinaires.CodeVeto = veto.CodeVeto _
                                                           And uneConsult.Etat <> 2)
            If requete.AsEnumerable.Count = 0 Then
                Return True
            End If

        End Get
    End Property




End Class
